
from flask import Flask
import Controllers
import os

import importlib


def register_controller(app: Flask, url_prefix):
    '''
    注册控制器
    ~~~~~

    author:czy
    :param url_prefix 路由前缀
    '''
    modules = __getAllControll()
    for item in modules:
        module = importlib.import_module(item, "Controllers")
        router = getattr(module, "router")
        url_prefix_module = getattr(module, "url_prefix")
        if not router == None or url_prefix_module == None:
            app.register_blueprint(
                router, url_prefix=url_prefix+url_prefix_module)


def __getAllControll():

    modules = []
    files = os.listdir("Controllers")
    allpar = dir(Controllers.Controllers)
    for item in files:
        if not item.startswith("__"):
            name, ext = os.path.splitext(item)
            modules.append("."+name)
    return modules
